from bs4 import BeautifulSoup
import requests


class Mooyuu(object):
    def __init__(self):
        pass

    #原始html
    def html(self,p):
        url = f'https://www.mooyuu.com/illust/index_{p}.html'
        req = requests.get(url).text
        self.soup = BeautifulSoup(req,'html.parser')

    #爬url
    def url(self,x):
        #取出url全部html
        all_url = self.soup.find_all('div',class_='p')
        #取出单个div
        html_url = all_url[x].img
        #取出url，并删除多余
        self.img_url = html_url.get('src').replace('thumb_300_0_','')
        print(self.img_url)

    #爬名字
    def name(self,x):
        x += 5
        #取全部html
        all_name = self.soup.find_all('div',class_='t')
        #name需要除去前5个，也就是说基数是5
        self.img_name = all_name[x].text
        print(self.img_name)

    #下载
    def download(self):
        img = requests.get(self.img_url)
        with open(f'{self.img_name}.jpg','wb') as f:
            f.write(img.content)
            

    #运行
    def run(self,start):
        #i为0~100 不包括100
        for n in range(1,start):
            self.html(n)
            for i in range(0,100):
                self.url(i)
                self.name(i)

                #self.download()  #为方便测试，注释。需要时打开

if __name__ == '__main__':
    obj = Mooyuu()
    print('页数不得 <= 1 ,2为一页，3为两页，以此类推。')
    i = int(input('页数>'))
    obj.run(i)

